package com.speed_run.utils;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class CommonUtils {
    private  static  final  String BASE_STR = "abcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 获取一个随机名,例如：20170903buspxe3i; 长度为16位字符
     * @return
     */
    public static String getRandomName(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 8 ; i++) {
            sb.append(BASE_STR.charAt(random.nextInt(BASE_STR.length())));
        }
        return new SimpleDateFormat("yyyyMMdd").format(new Date()).toString()+sb.toString();
    }

    public static String getPicName(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 24 ; i++) {
            sb.append(BASE_STR.charAt(random.nextInt(BASE_STR.length())));
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        System.out.println(getPicName());
    }

    @Test
    public void test(){
        try{
            String filepath = this.getClass().getClassLoader().getResource("")
                    .getPath()+"img"+File.separator+"avator"+File.separator
                    + CommonUtils.getPicName()+".txt";
            System.out.println(filepath);
            File f = new File(filepath);
            f.createNewFile();
            if(f.exists()){
                System.out.println("success");
            }else {
                System.out.println("fail");
            }
        }catch (Exception e){

        }

    }
}
